package model;

import java.util.Observer;
import java.util.Observable;

import model.building.Building;
import model.util.Vector3D;
import model.util.Object3D;

public abstract class BuildingFactory extends Observable implements Observer {

    /** Attributs */
    private City m_city;
    private boolean m_isEnabled;
    
    /** Constructeurs */
    public BuildingFactory(City city) { 
        m_city = city; 
        m_city.getEconomy().addObserver(this);
        update(null, null);
    }
    
    /** Accesseurs */
    public City getCity() { return m_city; }
    public abstract String getName();
    public abstract Object3D.Shape3D getShape3D();
    public abstract Building.Activity getDefaultActivity();

    /** Predicats */
    public boolean isEnabled() { return m_isEnabled; }

    public boolean canConstruct() { 
        return canBuy() && getDefaultActivity().canConstruct(getCity().getEconomy()); 
    }
    public abstract boolean canBuy();

    public final boolean canConstruct(Vector3D position) 
    { return getCity().canBuild(getShape3D(), position); }
    
    protected final boolean canBuy(double cost) 
    { return cost <= getCity().getEconomy().getQuantity(Ressources.Type.DOLLARS); }

    /** Methodes */
    public abstract Building construct(Vector3D position);
    
    protected final Building construct(Building building, double cost) {
        getCity().getEconomy().remove(Ressources.Type.DOLLARS, cost);
        getCity().add(building);
        building.construct();
        return building;
    }

    public void update(Observable economy, Object economyUpdate) {
        m_isEnabled = canConstruct();
        setChanged();
        notifyObservers();
    }
}
